<?php
/**
 * Created by PhpStorm.
 * User: lx
 * Date: 2020/1/3
 * Time: 9:33
 */
namespace app\api\controller;

use app\api\model\MtShopHouse;
use app\mtadminapi\model\MtOrder;
use app\util\ReturnCode;
use think\facade\App;
use \think\Request;
use app\command\controller\Testing;
use app\model\mt_user;
use app\command\controller\Generate;
use app\command\controller\Common;
use think\Db;

/**
 * Class User
 * @package app\api\controller
 * 小程序订单主类
 */
class Order extends Base
{
    public function __construct(){
        parent::__construct();
    }

    /**
     * @todo
     */
    public function createOrder(){
        $houseID = $this->request->get("houseID");
        if(!$houseID){
            return $this->error(ReturnCode::EMPTY_PARAMS,"缺少参数houseID");
        }
        $userID = $this->request->get("userID");
        if(!$userID){
            return $this->error(ReturnCode::EMPTY_PARAMS,"缺少参数userID");
        }
    }

    /**
     * 房态管理新增订单
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function newlyAddedOrder(){
        $rule = [
            'userID' => 'require|number',
            'shopID' => 'require|number',
            'houseID' => 'require|number',
            'userTel' => 'number',
            'idCard' => 'number',
            'userFrom' => 'require|number',
            'enterTime' => 'require',
            'stayOfTime' => 'number', // 几晚
            'orderPayMethod' => 'number',
        ];
        $msg = [];
        $data = $this->request->post();
        $verifiRes = (new Common())->VerifiString($rule, $msg, $data);
        if ($verifiRes['code'] == 2) {
            return $this->error(ReturnCode::EMPTY_PARAMS, $verifiRes['msg']);
        }
        $total = (new MtShopHouse())->where("shopID", $data['shopID'])->field("housePrice")->find();
        $data['enterTime'] = strtotime($data['enterTime']);
        $data['orderActualTotal'] = $data['orderTotal'] = $total;
        $data['orderCreateTime'] = time();
        $data['orderNo'] = (new Generate())->orderNumber($data['userID']);
        Db::startTrans();
        try {
            Db::table('mt_order')->insert($data,true);
            $userData = [
                /**
                 * @todo
                 */
                "userID" => $data['userID'],
                "orderNo" => $data['orderNo']
            ];
            Db::table('mt_order_user')->insert($userData);
            Db::table('mt_shop')->where('shopID', $data['shopID'])->setDec('peopleNum');
            Db::table('mt_shop_house')->where('houseID', $data['houseID'])->setDec('reserveNum');
            // 提交事务
            Db::commit();
            return $this->success("入库成功");
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->error(ReturnCode::DB_SAVE_ERROR,"入库失败");
        }
    }

    /**
     * 用户订单列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function UserOrderList(){
        $userID = $this->request->param("userID");
        if(!$userID){
            return $this->error(ReturnCode::EMPTY_PARAMS,"缺少参数");
        }
        $statusTerm = intval($this->request->get("statusTerm")); // 订单状态
        $p = intval($this->request->get("p")) ? intval($this->request->get("p")) : 1;
        $limit = intval($this->request->get("limit")) ? intval($this->request->get("limit")) : config("adminapi.ADMIN_LIST_DEFAULT");
        if($statusTerm){
            $where = ["orderState","=",$statusTerm];
        }else{
            $where = "1=1";
        }
        $orderList = (new MtOrder())->where("userID",$userID)->where($where)->page($p,$limit)->select();
        if(!$orderList){
            return $this->error(ReturnCode::DB_SAVE_ERROR,"查询失败");
        }
        return $this->success("查询成功",$orderList);
    }
    public function orderInfo(){
        $orderID = $this->request->get("orderID");
        $userID = $this->request->param("userID");
        if(!$orderID || $userID){
            return $this->error(ReturnCode::EMPTY_PARAMS,"缺少参数");
        }
        $orderInfo = (new MtOrder())->where(["id","=",$orderID],["userID","=",$userID])->find();
        if($orderInfo){
            return $this->error(ReturnCode::DB_READ_ERROR,"查询失败");
        }
        return $this->success("查询成功",$orderInfo);
    }
}